package com.example.busshop.controller;

import com.example.busshop.common.utils.TokenUtil;
import com.example.busshop.config.R;
import com.example.busshop.entity.User;
import com.example.busshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    TokenUtil tokenUtil;

    @GetMapping("/login")
    public String toLoginPage() {
        return "login";
    }

    @PostMapping("/loginCheck")
    @ResponseBody
    public Object login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        String token= tokenUtil.getToken(request);
        Object result = userService.loginCheck(user, response);
        map.put("token",token);
        map.put("result",result);
        return map;
    }
    @PostMapping("/add")
    @ResponseBody
    public User add(@RequestBody User user) {
       return userService.POST(user);
    }
    @PutMapping("/update")
    public User update(@RequestBody User user){
        return userService.POST(user);
    }

}